package rdf

import "uri"

type Node struct {spelling string; loc int; hash int}

type Matcher interface {
  AsNode() *Node
  AsVar() string
  Match(*Node) bool
}

var ANY Matcher = any{}

type any struct{}

func (any) AsNode() *Node { return nil }

func (any) AsVar() (result string) { return result }

func (any) Match(*Node) bool { return true }

func NewURI( u uri.URI ) *Node {
  return &Node{u.Spelling(), 0, hashOf(u.Spelling())}
}

func NewNode( spelling string ) *Node {
  loc, ch := 0, spelling[0]
  if ch == '<' { spelling = spelling[1:len(spelling)-1] }
  if ch == '"' { loc = 0 }
  return &Node{spelling, loc, hashOf( spelling )}
}

func hashOf( spelling string ) int {
  hash := 0
  for _, ch := range spelling { hash = hash * 31 + ch }
  return hash
}

func (n *Node) AsNode() *Node { return n }

func (n *Node) AsVar() (null string) { return null } 

func (n *Node) Match( other *Node ) bool { return n.Equals( other ) }

func (n *Node) String() string { return n.spelling }

func (n *Node) Hash() int { return n.hash }   

func (n *Node) Equals( other *Node ) bool {
  return n.spelling == other.spelling
}
